<?php

class loader{
	private $file = ' ';
	private $path = ' ';
	private $classes = array();

	public function load($path, $file, $classname = NULL) {
		try {
			$filepath = $path . '/' . $file . '.php';
			if (!isset ($classname)) $classname=$file; 
			if (isset ($this->classes[$classname]) )
			{
				return $this->classes[$classname];
			}
			else 
			{
				if (class_exists($classname))
					return $this->classes[$classname] = new $classname($this);	
				else 
				{
					if (file_exists($filepath))	
					{
						require_once($filepath);
						if (class_exists($classname))
							return $this->classes[$classname] = new $classname($this);
						else 
							throw new exception ('Invalid class name! ');					
					}
					else{
						
						throw new exception ('Invalid file name! ');}
				}		
			}
		}
		catch (Exception $e) {
			echo 'Error: ' . $e->getMessage();
		}
	}

	public function error(){}

	public function model($name){
		$name=ucfirst($name);
		$class_name = $name.'Model';
		return $this->load('model',$name,$class_name);
	}

	public function view($name){
		$name=ucfirst($name);
		$class_name = $name.'View';
		return $this->load('view',$name,$class_name);
	}

	public function controller($name){
		$name=ucfirst($name);
		$class_name = $name.'Controller';
		return $this->load('controller',$name,$class_name);
	}
	
	public function config($name){
		require("config.php");
		if (array_key_exists($name, $config)){
			return $config[$name];
		}
		
	}

}

//áéáéá